package ml.kupin.common.utils;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

/**
 * Created by 小柯(Allen Kerr) on 17.4.25 18:54.
 */
public class FastDfsClientUtils {
    //TrackerClient 对象
    private TrackerClient trackerClient = null;
    //TrackerServer 对象，通过 TrackerClient 获得
    private TrackerServer trackerServer = null;
    //StorageServer 的引用，可以是 null
    private StorageServer storageServer = null;
    //StorageClient1 对象
    private StorageClient1 storageClient = null;

    /**
     * 获取配置文件的绝对路径并创建storageClient对象
     *
     * @param conf
     * @throws Exception
     */
    public FastDfsClientUtils(String conf) throws Exception {
        if (conf.contains("classpath:")) {
            conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
        }
        ClientGlobal.init(conf);
        trackerClient = new TrackerClient();
        trackerServer = trackerClient.getConnection();
        storageServer = null;
        storageClient = new StorageClient1(trackerServer, storageServer);
    }

    /**
     * 上传文件方法
     *
     * @param fileName
     * @param extName
     * @param metas
     * @return
     * @throws Exception
     */
    public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
        return storageClient.upload_file1(fileName, extName, metas);
    }

    /**
     * 上传文件方法
     *
     * @param fileName
     * @return
     * @throws Exception
     */
    public String uploadFile(String fileName) throws Exception {
        return uploadFile(fileName, null, null);
    }

    /**
     * 上传文件方法
     *
     * @param fileName
     * @param extName
     * @return
     * @throws Exception
     */
    public String uploadFile(String fileName, String extName) throws Exception {
        return uploadFile(fileName, extName, null);
    }

    /**
     * 上传文件方法
     *
     * @param fileContent
     * @param extName
     * @param metas
     * @return
     * @throws Exception
     */
    public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
        return storageClient.upload_file1(fileContent, extName, metas);
    }

    /**
     * 上传文件方法
     *
     * @param fileContent
     * @return
     * @throws Exception
     */
    public String uploadFile(byte[] fileContent) throws Exception {
        return uploadFile(fileContent, null, null);
    }

    /**
     * 上传文件方法
     *
     * @param fileContent
     * @param extName
     * @return
     * @throws Exception
     */
    public String uploadFile(byte[] fileContent, String extName) throws Exception {
        return uploadFile(fileContent, extName, null);
    }


}
